import React, { Component } from 'react'
import './Detail.less'
import Header from '../../components/Header/Header'
import {reqgetgoodsinfo,reqcartadd} from '../../http/api'
import { $pre } from '../../http/http'
import {Toast,Button} from 'antd-mobile'
export default class Detail extends Component {
  constructor(props){
    super(props)
    let {id,type} = this.props.match.params;
    this.id =id;
    this.type=type-0;
    this.state={
      goods:{},
    }
  }
  componentDidMount(){
    this.getDetail();
  }
  async getDetail(){
    console.log(this.type);
    let {data:res} = await reqgetgoodsinfo({id:this.id,type:this.type});
    if(res.code===200){
      this.setState({
        goods:res.list[0] ? res.list[0] : {},
      })
    }
  }
  async addCart(){
    // console.log('加入购物车');
    let {uid} = JSON.parse(localStorage.getItem('isLogin'));
    let {id:goodsid} = this.state.goods;
    let {data:res} = await reqcartadd({uid,goodsid,type:this.type,num:1});
    if(res.code === 200){
      Toast.show({content:res.msg});
      this.props.history.push('/index/shop');
    }
  }
  render() {
    let {goods} = this.state
    return (
      <div className='detail'>
        <Header back title='商品详情'></Header>
        {goods.id ? (<div>
          <img src={$pre + goods.img} alt="" />
          <p>商品名称：{goods.goodsname}</p>
          <p>商品价格：￥{goods.price}</p>
          <div><Button shape='rounded' color='warning' onClick={()=> this.addCart()}>加入购物车</Button><Button shape='rounded' color='danger' >立即购买</Button></div>
        </div>) : (<div>暂无数据</div>)}
      </div>
    )
  }
}
